package com.example.shopping_li.fpage;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.shopping_li.Fragment.BaseFragment;
import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.example.shopping_li.goods.GoodsTypeBean;
import com.example.shopping_li.goods.TypeBean;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.lang.reflect.GenericArrayType;
import java.util.ArrayList;
import java.util.List;
public class FirstPageFragment extends BaseFragment {
        ViewPager typeVp,lbVp;
        LinearLayout typeLayout,lpLayout;
        List<ImageView>lbviewList;
        List<ImageView>lbPointList;//轮播下面的小点点
        Handler handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what==1) {
                    int currentItem = lbVp.getCurrentItem();
                    if (currentItem==lbviewList.size()-1) {
                        lbVp.setCurrentItem(0);
                    }else{
                        lbVp.setCurrentItem(currentItem+1);
                    }
                    handler.sendEmptyMessageDelayed(1,3000);
                }
            }
        };
        int totalPage;
        int pageSize=8;
        List<GridView>typeviewList;//分类的viewpager的数据源

        public FirstPageFragment() {
            // Required empty public constructor
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_first_page, container, false);
            initView(view);
            typeviewList=new ArrayList<>();
            setLBData();//设置轮播图显示数据
            setLbListener();
            handler.sendEmptyMessageDelayed(1,3000);
            connectNet(URLUtils.allCategory);//联网
            return view;
        }

    @Override
    public void onSuccess(String result) {

        TypeBean bean=new Gson().fromJson(result,TypeBean.class);
        List<TypeBean.DataBean> list = bean.getData();
totalPage=(int)Math.ceil(list.size()*1.0/pageSize);
for (int i=0;i<totalPage;i++){
    GridView gridView = new GridView(getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    params.setMargins(10,10,10,10);
    gridView.setLayoutParams(params);
    gridView.setNumColumns(pageSize/2);
    //设置监听事件
    //设置适配器
    FPTypeAdapter adapter=new FPTypeAdapter(getContext(),list,i,pageSize);
    gridView.setAdapter(adapter);
typeviewList.add(gridView);
}
//ViewPager适配器
        FPCatagoryAdapter catagoryAdapter=new FPCatagoryAdapter(typeviewList);
typeVp.setAdapter(catagoryAdapter);
    }
    private void setLbListener() {
            lbVp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
                @Override
                public void onPageSelected(int position) {
                    for (int i=0;i<lbPointList.size();i++){
                        lbPointList.get(i).setImageResource(R.mipmap.page__normal_indicator);

                    }
                    lbPointList.get(position).setImageResource(R.mipmap.page__selected_indicator);

                }
            });
    }

    private void setLBData() {
            lbviewList = new ArrayList<>();
            lbPointList = new ArrayList<>();
            int[]resId = {R.mipmap.ad1,R.mipmap.ad2};
            for (int i = 0;i<resId.length;i++){
                ImageView iv = new ImageView(getContext());
                iv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
                iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                iv.setImageResource(resId[i]);
                lbviewList.add(iv);
                //小点点初始化
                ImageView piv = new ImageView(getContext());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
                params.setMargins(8,0,8,0);
                piv.setLayoutParams(params);
                piv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                piv.setImageResource(R.mipmap.page__normal_indicator);
                lpLayout.addView(piv);
                lbPointList.add(piv);
            }
            lbPointList.get(0).setImageResource(R.mipmap.page__selected_indicator);
            LunboAdapter lunboAdapter = new LunboAdapter(lbviewList);
            lbVp.setAdapter(lunboAdapter);
        }

        private void initView(View view) {
            typeVp = view.findViewById(R.id.frag_fp_vp_type);
            lbVp = view.findViewById(R.id.frag_fp_vp_lb);
            typeLayout = view.findViewById(R.id.frag_fp_layout_p1);
            lpLayout = view.findViewById(R.id.frag_fp_layout_p2);
        }

    }







